package com.lib.core.base


import android.graphics.Color
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import com.corelibs.expand.showToast
import com.lib.core.utils.AppManager
import com.lib.core.view.LoadingDialog


import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.zackratos.ultimatebarx.ultimatebarx.java.UltimateBarX
import java.lang.reflect.ParameterizedType


abstract class BaseActivity<VB : ViewBinding, VM : BaseViewModel> : AppCompatActivity() {



    protected lateinit var binding: VB
        private set

    protected lateinit var viewModel: VM
        private set



    private val loadingDialog: LoadingDialog by lazy { LoadingDialog(this) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // ---- ViewBinding 反射 ----
        val type = (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments
        val vbClass = type[0] as Class<*>
        val vmClass = type[1] as Class<VM>

        val method = vbClass.getMethod("inflate", LayoutInflater::class.java)
        @Suppress("UNCHECKED_CAST")
        binding = method.invoke(null, layoutInflater) as VB
        setContentView(binding.root)

        // ---- ViewModel 自动创建 ----
        viewModel = ViewModelProvider(this)[vmClass]

        AppManager.getAppManager().addActivity(this)
        UltimateBarX.statusBarOnly(this)
            .fitWindow(true)
            .color(Color.WHITE)
            .light(true)
            .apply()
        initView()
        initData()
        registerUIChange()
        observeData()
    }




    abstract fun initView()

    open fun observeData() {}

    open fun initData() {}


    private fun registerUIChange() {
        viewModel.uiModel.showDialog.observe(this) {
            if (it) {
                showLoadingDialog()
            } else {
                dismissLoadingDialog()
            }
        }
        viewModel.uiModel.finishRefresh.observe(this) {
            finishRefresh()
        }
        viewModel.uiModel.finishLoadMore.observe(this) {
            finishLoadMore(it)
        }
    }


    private fun showLoadingDialog() {
        if (!loadingDialog.isShowing)
            loadingDialog.show()
    }

    private fun dismissLoadingDialog() {
        loadingDialog.dismiss()
    }

    fun getText(textView: TextView): String {
        if (textView.text.isEmpty()) {
            return ""
        }
        return textView.text.trim().toString()
    }

    open fun getRefreshLayout(): RefreshLayout? {
        return null
    }

    fun checkText(textView: TextView, hint: String): Boolean {
        if (TextUtils.isEmpty(getText(textView))) {
            showToast(hint)
            return true
        }
        return false
    }

    fun checkText(textView: TextView, hint: Int): Boolean {
        if (TextUtils.isEmpty(getText(textView))) {
            showToast(hint)
            return true
        }
        return false
    }

    protected open fun finishRefresh() {
        getRefreshLayout()?.finishRefresh()
    }

    protected open fun finishLoadMore(haveData: Boolean) {
        if (haveData)
            getRefreshLayout()?.finishLoadMore()
        else
            getRefreshLayout()?.finishLoadMoreWithNoMoreData()
    }


    override fun onDestroy() {
        super.onDestroy()
        AppManager.getAppManager().finishActivity(this)
    }
}